/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/11/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.workbench.struts.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

import com.application.systemmanager.entity.Notice;
import com.application.systemmanager.entity.SystemConfig;
import com.application.systemmanager.service.SystemManagerService;
import com.application.usertable.entity.Usertable;
import com.application.workbench.commons.Constants;
import com.application.workbench.commons.DispatchPage;
import com.application.workbench.struts.form.WorkBenchForm;
import com.manager.BaseAction;
import com.manager.commons.CommonUtil;
import com.manager.commons.page.PageBean;
import com.manager.workflow.core.WorkFlowCommon;

/**
 * 
 * @author 王浩
 *
 */
public class WorkBenchAction extends BaseAction {



	private SystemManagerService systemManagerService;
	
	private WorkFlowCommon workFlowCommon;
	
//	private ProjectworksheetsService projectworksheetsService;
//	
//	private PatentService patentService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward enterWorkBench(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("enterWorkBench" + "开始");
		WorkBenchForm workBenchForm = (WorkBenchForm) form;
		
		HttpSession session = request.getSession();
		
		if (CommonUtil.getCurrentUser(request)!=null) {
			
		String actorId = CommonUtil.getCurrentUser(request).getId();
		Map conditionsMap = new HashMap();
		conditionsMap.put("actorId", actorId);
		
//		Usertable currentUser = new Usertable();
//		String rname = null;
//		String org = null;
//		String role = "xmjl";
//		if(actorId.equals("yfjl")){
//			rname = "卞红航";
//			org = "研发部经理";
//			role = "yfjl";
//        }else if(actorId.equals("yfzxfzr") || actorId.equals("szfzr") || actorId.equals("hzfzr") || actorId.equals("bjfzr")){
//        	rname = "卞红航";
//        	org = "研发中心负责人";
//        	role = "yfzxfzr";
//        }else if(actorId.equals("fgfz")){
//        	rname = "袁志巍";
//        	org = "分管副总";
//        	role = "fgfz";
//        }else if(actorId.equals("xmjl")){
//        	rname = "0";
//        	org = "项目经理";
//        	role = "xmjl";
//        }
//		currentUser.setRole(role);
//		currentUser.setRname(rname);
//		currentUser.setOrg(org);
//		
//		request.getSession().setAttribute("currentUser", currentUser);
		
        Usertable user = CommonUtil.getCurrentUser(request);
		
	
		if(user.getIsZJL().equals("true")){
			// 代办事宜
			List workItemsList = workFlowCommon.getWorkItemListZJL(conditionsMap, null);
			if(workItemsList!=null && workItemsList.size()>10){
				workItemsList = workItemsList.subList(0, 9);
			}
			request.setAttribute("workItemsList", workItemsList);
		}else{
			// 代办事宜
			List workItemsList = workFlowCommon.getWorkItemList(conditionsMap, null);
			if(workItemsList!=null && workItemsList.size()>10){
				workItemsList = workItemsList.subList(0, 9);
			}	     
			request.setAttribute("workItemsList", workItemsList);
		}
		
		// 已办事宜
		List workItemsHisList = workFlowCommon.getHaveDoneWorkItemList(conditionsMap, null);
		if(workItemsHisList!=null && workItemsHisList.size()>10){
			workItemsHisList = workItemsHisList.subList(0, 9);
		}
//
//		// 通知公告
		List noticeList = systemManagerService.findWorkBenchNoticeList();
//
		Map queryMap = new HashMap();
		queryMap.put("actorId", actorId);
		queryMap.put("type",new Long(10012));
		SystemConfig config = systemManagerService.findSystemConfigByCode("4100102");	
		int diff = 24;
		if(StringUtils.isNotBlank(config.getConfigValue())){
			diff = Integer.valueOf(config.getConfigValue());
		}
		queryMap.put("diff",diff);
//		// 预警信息
		List earlyWarningList = workFlowCommon.getAllWarnTask(queryMap, null);
		if(earlyWarningList!=null && earlyWarningList.size()>10){
			earlyWarningList = earlyWarningList.subList(0, 9);
		}

		request.setAttribute("workItemsHisList", workItemsHisList);
		
		request.setAttribute("noticeList", noticeList);
		request.setAttribute("earlyWarningList", earlyWarningList);
		}
		log.debug("enterWorkBench" + "结束");
		return mapping.findForward(Constants.SUCCESS);
	}

	
	public ActionForward viewFlow(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewFlow");
		String bussId = request.getParameter("bussId");
		String bussType = request.getParameter("bussType");
		String processinstanceId = workFlowCommon.getProcessInstanceId(bussId, bussType);
		request.setAttribute("processinstanceId", processinstanceId);
		return mapping.findForward(Constants.SUCCESS);
	}
	
	/**
	 * 打开代办项业务审核界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward openForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("openForm");
		WorkBenchForm workBenchForm = (WorkBenchForm) form;
		String[] res = workFlowCommon.getFormUrlAndProcessVar(workBenchForm.getWorkitemID());
		String bussId = res[1];
        String bussType = res[2];
        String processId = res[3];
        String auditRole = res[4];
        String viewUrl = res[5];
        String url = null;
        if(StringUtils.isNotBlank(viewUrl)){
        	url = viewUrl;
        }else{
        	url = DispatchPage.getPageUrl(Long.valueOf(bussType));
        }
        
		ActionRedirect redirect = new ActionRedirect(url); 
		redirect.addParameter("bussId",bussId);
		redirect.addParameter("processId",processId);
		redirect.addParameter("workItemId",workBenchForm.getWorkitemID());
		redirect.addParameter("bussType",workBenchForm.getType());
		redirect.addParameter("auditRole",auditRole);
		return redirect;
	}
	
	
	/**
	 * 跳转到具体的已办业务页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward goToBussinessView(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("goToBussinessView" + "开始");
		WorkBenchForm workBenchForm = (WorkBenchForm) form;
		String[] res = workFlowCommon.getBussVar(workBenchForm.getWorkitemID());
		String bussId = res[0];
        String bussType = res[1];
        String processId = res[2];
        String url = DispatchPage.getViewUrl(Long.valueOf(bussType));
        url = url + bussId + "&processId=" + processId;
		ActionForward forware = new ActionForward(url);
		return forware;
	}

	/**
	 * 跳转到具体的已办业务页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward goToBussinessViewZJL(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("goToBussinessViewZJL" + "开始");
		WorkBenchForm workBenchForm = (WorkBenchForm) form;
		
		String bussId = workBenchForm.getBussinessID();
        String bussType = String.valueOf(workBenchForm.getTypeName());
        String processId = workBenchForm.getInstanceID();
        String url = DispatchPage.getViewUrl(Long.valueOf(bussType));
        url = url + bussId + "&processId=" + processId;
		ActionForward forware = new ActionForward(url);
		return forware;
	}
	
	
	/**
	 * 查看通知
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward showNotice(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("showNotice");
		WorkBenchForm workBenchForm = (WorkBenchForm) form;
		Notice notice = new Notice();
		notice.setId(Long.valueOf(workBenchForm.getBussinessID()));
		notice = this.systemManagerService.getNotice(notice);
		request.setAttribute(Constants.NOTICE, notice);
		return mapping.findForward(Constants.SHOW_NOTICE);
	}

	/**
	 * 代办事宜列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getWorkItemList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("getWorkItemList" + "开始");
		WorkBenchForm workBenchForm = (WorkBenchForm) form;
		Usertable user = CommonUtil.getCurrentUser(request);
		
		String actorId = user.getId();
		Map conditionsMap = new HashMap();
		conditionsMap.put("actorId", actorId);
		conditionsMap.put("type",workBenchForm.getType());
		List workItemList = new ArrayList();
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		if(user.getIsZJL().equals("true")){
			resultSize = workFlowCommon.getAllWorkItemCountZJL(conditionsMap);
			pageBean = PageBean.getPageBean("workItem", resultSize, request);
			workItemList = workFlowCommon.getWorkItemListZJL(conditionsMap, null);
		}else{
			resultSize = workFlowCommon.getAllWorkItemCount(conditionsMap);
			pageBean = PageBean.getPageBean("workItem", resultSize, request);
			workItemList = workFlowCommon.getWorkItemList(conditionsMap, pageBean);
		}
		request.setAttribute("workItemList", workItemList);

		log.debug("getWorkItemList" + "结束");

		return mapping.findForward(Constants.SUCCESS);
		
	}

	/**
	 * 已办事宜列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getWorkItemHisList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("getWorkItemList" + "开始");
		WorkBenchForm workBenchForm = (WorkBenchForm) form;
		String actorId = CommonUtil.getCurrentUser(request).getId();
		Map conditionsMap = new HashMap();
		conditionsMap.put("actorId", actorId);
		conditionsMap.put("type",workBenchForm.getType());
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List workItemHisList = null;
		resultSize = workFlowCommon.getAllHaveDoneWorkItemCount(conditionsMap);
		pageBean = PageBean.getPageBean("workItemHis", resultSize, request);
		workItemHisList = workFlowCommon.getHaveDoneWorkItemList(conditionsMap, pageBean);

		request.setAttribute("workItemHisList", workItemHisList);

		log.debug("getWorkItemHisList" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 预警信息列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getWarningList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("getWarningList" + "开始");
		WorkBenchForm workBenchForm = (WorkBenchForm) form;
//		SystemConfig warningConfig = systemManagerService.findSystemConfigByCode(Constants.WARNING_CONFIG_ACCEPTANCE);
//		SystemConfig warningPatent = systemManagerService.findSystemConfigByCode(Constants.WARNING_CONFIG_PATENT);
//		HttpSession session = request.getSession();
//		workBenchForm.setUser(this.getCurrUserInfo(request));
//		workBenchForm.setWarningAppance(warningConfig.getConfigValue());
//		workBenchForm.setWarningPatent(warningPatent.getConfigValue());
		String actorId = CommonUtil.getCurrentUser(request).getId();
		Map queryMap = new HashMap();
		queryMap.put("actorId", actorId);
		queryMap.put("type",new Long(10012));
		
		SystemConfig config = systemManagerService.findSystemConfigByCode("4100102");	
		int diff = 24;
		if(StringUtils.isNotBlank(config.getConfigValue())){
			diff = Integer.valueOf(config.getConfigValue());
		}
		
		
		queryMap.put("diff",diff);
//		// 预警信息
		List earlyWarningList = workFlowCommon.getAllWarnTask(queryMap, null);
		if(earlyWarningList!=null && earlyWarningList.size()>10){
			earlyWarningList = earlyWarningList.subList(0, 9);
		}
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List warningList = null;
		resultSize = workFlowCommon.getAllWarnTaskCount(queryMap);
		pageBean = PageBean.getPageBean("warning", resultSize, request);
		warningList = workFlowCommon.getAllWarnTask(queryMap, pageBean);
		request.setAttribute("warningList", warningList);
		log.debug("getWarningList" + "结束");
		return mapping.findForward(Constants.SUCCESS);
	}
    
	


	
	

	public SystemManagerService getSystemManagerService() {
		return systemManagerService;
	}

	public void setSystemManagerService(SystemManagerService systemManagerService) {
		this.systemManagerService = systemManagerService;
	}

	public WorkFlowCommon getWorkFlowCommon() {
		return workFlowCommon;
	}

	public void setWorkFlowCommon(WorkFlowCommon workFlowCommon) {
		this.workFlowCommon = workFlowCommon;
	}

//	public void setProjectworksheetsService(ProjectworksheetsService projectworksheetsService) {
//		this.projectworksheetsService = projectworksheetsService;
//	}
//
//	public void setPatentService(PatentService patentService) {
//		this.patentService = patentService;
//	}

}
